package com.yunxiao.cloud.auth.server.support;

import com.alibaba.fastjson.JSON;
import com.yunxiao.cloud.auth.server.entity.SysPermission;
import com.yunxiao.cloud.auth.server.entity.SysRole;
import com.yunxiao.cloud.auth.server.entity.SysUser;
import com.yunxiao.cloud.auth.server.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @description: AfterStartRunner
 * @date: 2019/12/20 14:12
 * @author: yunxiao
 * @version: 1.0
 */
//@Component
//@Order(1)
public class AfterStartRunnerTask implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(AfterStartRunnerTask.class);

    @Resource
    SysUserService sysUserService;

    @Override
    public void run(String... args) throws Exception {
        Thread.sleep(2000L);
        SysRole admin = new SysRole();
        admin.setRoleCode("ADMIN");
        admin.setRoleName("管理员");
        SysRole developer = new SysRole();
        developer.setRoleCode("DEVELOPER");
        developer.setRoleName("开发者");

        SysPermission p1 = new SysPermission();
        p1.setCode("memberExport");
        p1.setName("会员列表导出");
        p1.setUrl("/member/export");

        SysPermission p2 = new SysPermission();
        p2.setCode("BookList");
        p2.setName("图书列表");
        p2.setUrl("/book/list");


        admin.setSysPermissionList(Arrays.asList(p1, p2));
        admin.setPermissionList(JSON.toJSONString(admin.getSysPermissionList()));
        developer.setSysPermissionList(Arrays.asList(p1));
        developer.setPermissionList(JSON.toJSONString(developer.getSysPermissionList()));

        SysUser sysUser = new SysUser();
        sysUser.setId(2L);
        sysUser.setUsername("lisi");
        sysUser.setPassword("$2a$10$aZDOWYEvK06TrxN6g0Mta.X3gtnj1sHPReRic5YRcOiXl4yMctwS6");
        sysUser.setSysRoleList(Arrays.asList(developer));
        sysUser.setRoleList(JSON.toJSONString(sysUser.getSysRoleList()));

        sysUserService.save(sysUser);
        String usrStr = JSON.toJSONString(sysUser);
        System.out.println(usrStr);
    }
}
